package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.Goods_id;
import com.gouaixin.bee.beeframework2.bean.Pay;
import com.gouaixin.bee.beeframework2.extra.myutils.YHttpUtil;
import com.gouaixin.bee.beeframework2.fragment.Refund_fragment;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.FileUtils;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

/**
 * Created by Administrator on 2015/3/10.
 */
//vip特权界面
public class B14_vipTequanActivity extends FragmentActivity {
    Context context=B14_vipTequanActivity.this;

    //顶部
    ImageView back;//返回
    TextView title;//标题

    TextView viptv01;//查看隐私照片
    TextView viptv04;//查看联系方式
    TextView viptv09;//更精准搜索条件

    TextView kt01,kt02,kt03,kt04;//立即开通

    String order="";//订单名
    String price="";//订单价格



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b14_viptequan);

        cid= PreferencesUtils.getString(context, "cid");

        initView();

        setData();

        setListener();

        initWebView();//设置网页

    }
    WebView webView;
    String html01;

    private void initWebView() {
        webView= (WebView) findViewById(R.id.webView);
        html01 = FileUtils.readAssest(this, "html/vip.html");
        replaceHtmlString(html01);
    }

    /**
     * 替换Html中的标记符
     */
    private void replaceHtmlString(final String html) {

        //根据是否充值过，判断显示的充值项目(通过替换Html中的标记符@showFlag04)
        MemberModel.haveRecharge(context, new MemberModel.HaveRechargeListener() {
            @Override
            public void onResult(boolean haveRecharge) {
                //如果充值过
                if (haveRecharge) {
                    html01 = html.replace("@showFlag04","none");
                    YHttpUtil.loadAssetsHtmlString(context, webView, html01, "vip");
                }
                //如果没充值过
                else {
                    html01 = html.replace("@showFlag04","block");
                    YHttpUtil.loadAssetsHtmlString(context, webView, html01, "vip");
                }
            }
        });

//        html = html.replace("@price", price[flag]);
//        html = html.replace("@number", number[flag]);
//        html = html.replace("@instruction", instruction[flag]);
//        html = html.replace("@icon", icon[flag]);
//        html = html.replace("@flag", flag+"");

//        html = html.replace("#", "%23");
//        html = html.replace("%", "%25");
//        html = html.replace("\\", "%27");
//        html = html.replace("?", "%3f");
//        return html;
    }

    @JavascriptInterface
    public void clickOnAndroid(final int flag) {
        Log.e("Demo", "js调用了java函数:flag=" + flag);
//        Toast.makeText(this, "js调用了java函数:flag=" + flag, Toast.LENGTH_SHORT).show();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                setListenerInJs(flag);
            }
        });
    }

    /**
     * 实现js接口的点击事件
     *
     * @param flag
     */
    private void setListenerInJs(int flag) {
        Intent it;
        switch (flag){
            case 1:
                order_name="终身VIP";
                order_price="1888";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.VIP_1888);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
            case 2:
                order_name="12个月VIP";
                order_price="500";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.VIP_500);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
            case 3:
                order_name="3个月VIP";
                order_price="200";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.VIP_200);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
            case 4:
                order_name="1个月VIP";
                order_price="100";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.VIP_100);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
        }


    }


    private void setData() {
        viptv01.setText(Html.fromHtml("<font color=\"#a3a3a3\">" +"查看隐秘照片精华，" + "</font><font color=\"#FF0541\">"+"（TA上传的精华私密照片）"+"</font>"));
        viptv04.setText(Html.fromHtml("<font color=\"#a3a3a3\">" +"查看心仪的TA" + "</font><font color=\"#FF0541\">"+"联系方式（QQ号、手机号、微信号）"+"</font>"));
        viptv09.setText(Html.fromHtml("<font color=\"#a3a3a3\">" +"可以通过" + "</font><font color=\"#FF0541\">"+"年龄、身高、地区、星座、收入"+"</font><font color=\"#a3a3a3\">"+"等多个条件来找到你喜欢的MM、GG"+"</font>"));
    }

    private String order_name="",order_price="";
    private int goods_id;//商品的唯一id
    Pay bean=new Pay();
    String cid="";
    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

//        kt01.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Pay bean=new Pay();
//                PayModel.pay(context, bean);
//            }
//        });
//
//        kt02.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                MemberModel.vipSuccess(context, VipType.Type_500);
//                order_name="VIP特权"+"12"+"个月";
//                order_price="500";
//                Intent it=new Intent(context,F1_PaySelectActivity.class);
//                it.putExtra("name",order_name);
//                it.putExtra("price",order_price);
//                startActivity(it);
//
//            }
//        });
//
//        kt03.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                MemberModel.vipSuccess(context, VipType.Type_200);
//            }
//        });
//
//        kt04.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                MemberModel.vipSuccess(context, VipType.Type_100);
//            }
//        });


    }

    private void initView() {

        back= (ImageView) findViewById(R.id.back);
        title= (TextView) findViewById(R.id.title);

        viptv01= (TextView) findViewById(R.id.viptv01);
        viptv04= (TextView) findViewById(R.id.viptv04);
        viptv09= (TextView) findViewById(R.id.viptv09);

//        kt01= (TextView) findViewById(R.id.kt01);
//        kt02= (TextView) findViewById(R.id.kt02);
//        kt03= (TextView) findViewById(R.id.kt03);
//        kt04= (TextView) findViewById(R.id.kt04);

        Refund_fragment tuikuan_fragment= (Refund_fragment) getSupportFragmentManager().findFragmentById(R.id.tuikuan_fragment);
        tuikuan_fragment.setFlag(Refund_fragment.FatherType_Hongniang);//设置fragment的父标签

    }



}
